# https://www.ieama.com/
# https://www.ieama.com/yingshi/2.html
# 影视网
import os
import re
import shutil

import requests
from util.headers import get_headers_init_user_agent


root = 'static/影视网/'
if os.path.exists(root):
    shutil.rmtree(root)
os.makedirs(root)


# url = 'https://www.ieama.com/yingshi/2.html'
url = 'https://www.ieama.com/'
response = requests.get(url)
# print(response.status_code)
content = response.content.decode('utf-8')
# print(content)
items = re.findall(r'<a href="/movie/(.*?).html" title="(.*?)" class="module-item-pic"><img class="lazyload" data-src="https://pic3.yzzyimages.com/upload/vod/(.*?).jpg" src="/statics/loading.png" alt="(.*?)"><div class="loading"></div></a>', content)
# items = re.findall(r'<a href="/movie/(.*?).html" title="(.*?)" class="module-item-pic"><img class="lazyload" data-src="https://tu.ikgambwqeqnv.com/upload/vod/(.*?).jpg" src="/statics/loading.png" alt="(.*?)"><div class="loading"></div></a>', content)
# print(items)
for item in items:
    # print(item)
    title = item[1]
    thumb_url = f'https://viptulz.com/upload/vod/{item[2]}.jpg'
    headers = get_headers_init_user_agent()
    headers.update({
        'referer': url
    })
    thumb_response = requests.get(thumb_url, headers=headers)
    with open(f"{root}/{title}.jpg", "wb") as f:
        f.write(thumb_response.content)